gpNamespace.ParentTilesLayer = cc.Layer.extend({
    sprite: null,
    player: null,
    table: null,
    startPoint: cc.p(640, 100),
    headPoint: cc.p(71, 345),
    headImgPoint: cc.p(55, 95),
    headArmaturePoint: cc.p(78, 600),
    chuPointStart: cc.p(640, 360),
    chuPoint: cc.p(0, 0),
    animationPosition: cc.p(640, 240),
    tilesLayer: null,
    nextChuLine: false,
    chuLineZOrder: 10,
    actionsLayerTag: 12,
    showTileTag: 1000,
    isOnline: false,
    lineCount: 12,
    animationZorder: 1001,
//    headBackground: "#mj_g_62u.png",
    moneyLabel: null,
 
    preparePosition: cc.p(153, 120),
    myHeadNode: null,
    remains: 0,
    currentChuPosition: null,
    fapaiEnd: false,
    ming: null,
    headPosition: [cc.p(71, 343), cc.p(1206, 527), cc.p(71, 527)],
    mingPosition: [cc.p(168, 300), cc.p(1099, 475), cc.p(168, 475)],
//    counterPosition: [cc.p(168, 250),cc.p(1099, 420), cc.p(600,560), cc.p(168, 440)],
    index: 0,
    tiles: [],

    trustMenuTag: 11,
    trustFrontTag: 12,
    trustArmatureTag: 13,
    trustBannerTag: 14,
    trustLabelTag: 15,
    trustDotTag: 16,

    ctor: function (player, table) {
        this.player = player;
        this.table = table;
        this.remains = 0;
        this.isOnline = true;
        
        player.playLayer = this;

        this.startPoint = cc.p(640, 80);

        this._super();
        this.init();
    },

    init: function () {
        this._super();
        var myTiles = this.player.handTiles;
        
        cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkgamesenceRes.Pdkgamesence_plist);
        cc.spriteFrameCache.addSpriteFrames(gpNamespace.gameRes.Game_plist);
        this.initPosition();
        this.drawHead();
        this.drawTiles(myTiles);
    },

    initPosition: function () {
        
    },

    drawHead: function () {
    },

    removeHead: function () {
    },
	
    headCallbackFunction: function (sender) {
        cc.log("个人信息");

//        gpNamespace.playEffect(gpNamespace.soundRes.ButtonEffect, false);

        tyNamespace.setMenuEnable(sender);

        var data = sender.getUserData();
        var player = data[0];
        var orderIndex = data[1];

        var runningScene = cc.director.getRunningScene();
        var tyModelLayer1 = new gpNamespace.TyModelLayer1();
        var tyModelLayer = new gpNamespace.TyModelLayer();
        tyModelLayer.setTouchArea(cc.size(597, 413), cc.p(337, 170));
        tyModelLayer1.addChild(tyModelLayer);
        runningScene.addChild(tyModelLayer1, 10);

        var innerUserInfoLayer = new gpNamespace.InnerUserInfoLayer(player);
        innerUserInfoLayer.attr({
            x: 0,
            y: cc.winSize.height
        });
        tyModelLayer.addChild(innerUserInfoLayer);

        innerUserInfoLayer.runAction(cc.moveBy(20 / 60, cc.p(0, -cc.winSize.height)).easing(cc.easeBackOut()));

        xinyou.netManager.getInnerUserInfo(orderIndex);
    },
    
    addGrabImage: function (useGood) {
        var imageName = "#cdd_jm_023u.png";
        if (useGood)
            imageName = "#cdd_jm_024u.png";

        var grabImage = new cc.Sprite(imageName);
        grabImage.setPosition(89, 121);
        this.myHeadNode.addChild(grabImage, 1, "grabImage");
    },

    //显示画牌
    directShowTiles: function (myTiles) {
        this.startPoint.x -= myTiles.length * this.tileSpace / 2;

        for (var i = 0; i < myTiles.length; i++) {
            var tile = myTiles[i];
            tile.setBelong(this.player);

            cc.log(tile.getImg());

            var tileSprite = new cc.Sprite(tile.getImg());
            tileSprite.setScale(0.8);
            tile.sprite = tileSprite;

            this.setTileSpritePosition(i, tileSprite);

            var tag = 100 + i;

            if (this.getChildByTag(tag) != null)
                this.removeChildByTag(tag);

            this.addChild(tileSprite, i, tag);
        }
    },
    
    //画牌
    drawTiles: function (myTiles) {
    	cc.log("drawTiles myTiles length:" + myTiles.length);
        if (this.player.isShowCards) {
            this.showTiles(myTiles);

            return;
        }

        var card = new cc.Sprite(gpNamespace.pdkgame.Game_blacktile);
        card.setPosition(this.startPointAp);
        this.addChild(card, 2, "backCard");

        //其余三家剩余牌数
        var shengYu = new tyNamespace.NumLayer(1, 13, 45);
        shengYu.setScale(0.8);
        shengYu.setPosition(this.startPointAp.x - 128, this.startPointAp.y - 72);
        this.addChild(shengYu, 2, "shengYu");

        if (currentNetManager.isReconnnect) {
            shengYu.reloadNum(myTiles.length);
            this.remains = myTiles.length;

            return;
        }
        
        this.fapaiOther();
    },
    
    //其余三家发牌数
    fapaiOther: function () {
    	cc.log("fapaiOther");
    	
        var self = this;

        this.reloadRemains(1, true);

        if (this.remains == gpNamespace.GAME_CARD_COUNT)
            return;

        this.runAction(cc.sequence(cc.delayTime(3 / gpNamespace.GAME_CARD_COUNT), cc.callFunc(function () {
            self.fapaiOther();
        })));
    },

    removeBackCard: function () {
        var backCard = this.getChildByName("backCard");
        var shengYu = this.getChildByName("shengYu");

        if (backCard) {
            backCard.removeFromParent(true);
            shengYu.removeFromParent(true);
        }
    },

    showTiles: function (myTiles) {
        this.index = 0;
        this.tiles = myTiles;

        if (currentNetManager.isReconnnect)
            this.directShowTiles(myTiles);
        else
            this.addShowTile();
    },

    addShowTile: function () {
        var self = this;

        if (this.tiles.length <= this.index) {
            self.player.playLayer.fapaiEnd = true;
            cc.log("###self.player.playLayer.fapaiEnd = true;");

            this.scheduleOnce(function(){
                var gameBeginAvi = new ccs.Armature("mjdh_small_01");
                gameBeginAvi.getAnimation().playWithIndex(1);
                gameBeginAvi.getAnimation().setSpeedScale(1);
                gameBeginAvi.setScale(1);
                gameBeginAvi.setAnchorPoint(0.5, 0.5);
                gameBeginAvi.setPosition(640, 360);
                this.addChild(gameBeginAvi,10);
                gameBeginAvi.getAnimation().setMovementEventCallFunc(this.freeAnimationCallBackRemove, this);
            },2);
            this.scheduleOnce(function(){
                gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.BeginEffect, false);
            },2.5);
             return;
        }

        this.makeCard();

        for (var a = 0; a < this.index; a++) {
            this.tiles[a].sprite.runAction(cc.moveBy(2 / 17, -this.tileSpace / 2, 0));
        }

        this.setStartPoint();

        this.index++;

        this.runAction(cc.sequence(cc.delayTime(2 / 17), cc.callFunc(function () {
            self.addShowTile();
        })));
    },

    freeAnimationCallBackRemove: function (armature, movementType, movementID) {
        this.scheduleOnce(function(){
            if (movementType == ccs.MovementEventType.complete) {
                armature.removeFromParent(true);
            }
        },1);
    },

    makeCard: function () {
        var index = this.index;
        var tile = this.tiles[index];

        tile.setBelong(this.player);

        cc.log(tile.num + "," + tile.type + "," + tile.getImg());

        var tileSprite = new cc.Sprite(tile.getImg());
        tileSprite.setScale(0.8);
        tile.sprite = tileSprite;

        tileSprite.setPosition(this.startPoint.x + index * this.tileSpace, this.startPoint.y);

        var tag = 100 + index;

        this.addChild(tileSprite, index, tag);
    },

    setStartPoint: function () {
        this.startPoint.x -= this.tileSpace / 2;
    },

    reloadTiles: function (tiles) {
        gpNamespace.CardActions.sort(tiles);

        var myTiles = this.player.handTiles;

        this.player.setHandCards(myTiles.concat(tiles), true);

        for (var a = 0; a < myTiles.length; a++) {
            var tileSprite = new cc.Sprite(myTiles[a].getImg());

            tileSprite.setPosition(myTiles[a].sprite.getPosition());
            var tag = 100 + a;
            this.addChild(tileSprite, a, tag);

            myTiles[a].removeFromParent();
            myTiles[a].sprite = tileSprite;
        }

        this.showDiTiles(tiles);
    },

    showDiTiles: function (tiles) {
        this.index = tiles[0].index;
        this.tiles = this.player.handTiles;

        this.addDiTile(tiles);
    },

    addDiTile: function (tiles) {
        var self = this;

        this.makeCard();

        var index = this.index;
        var tile = this.tiles[index];

        var zorder = [];

        //cc.log("++++++++++startPoint:" + this.startPoint.x);

        for (var a = 0; a < index; a++) {
            this.tiles[a].sprite.runAction(cc.moveBy(1 / 3, -this.tileSpace / 2, 0));
            zorder.push(this.tiles[a].sprite.getLocalZOrder());
        }

        tile.sprite.setLocalZOrder(Math.max.apply(this, zorder));

        for (var a = this.index + 1; a < this.tiles.length; a++) {
            if (this.tiles[a].sprite) {
                this.tiles[a].sprite.runAction(cc.moveBy(1 / 3, this.tileSpace / 2, 0));
            }
        }

        this.setStartPoint();

        if (this.index == tiles[tiles.length - 1].index) {
            self.player.playLayer.fapaiEnd = true;
            //self.player.playLayer.addMyListener();
            return;
        }

        for (var a = 0; a < tiles.length - 1; a++) {
            if (this.index == tiles[a].index) {
                this.index = tiles[a + 1].index;
                break;
            }
        }

        this.runAction(cc.sequence(cc.delayTime(1 / 3), cc.callFunc(function () {
            self.addDiTile(tiles);
        })));
    },

    reloadRemains: function (num, isAdd) {
        cc.log("reloadRemains this.remains" + this.remains + ",num" + num);
        if (isAdd)
            this.remains += num;
        else
            this.remains -= num;

        var shengYu = this.getChildByName("shengYu");

        if (shengYu) {
            if (this.remains == 0 && !this.player.isShowCards) {
                this.removeBackCard();
            } else {
            	if (this.remains < 10)
            		shengYu.length = 1;
            	
            	shengYu.reloadNum(this.remains);
            }
        }
        gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.FapaiEffect, false);
        cc.log("reloadRemains end");
    },
    
    removeRobot:function(){
    	cc.log("取消托管");
    	
    	if (this.player.status == tyNamespace.PlayerStatus.AutoPlay)
    		this.player.status = tyNamespace.PlayerStatus.Playing;
    	
    	var tuoguanLayer = this.getChildByName("tuoguanLayer");  //设置取消托管按钮隐藏
    	if (tuoguanLayer)
    		tuoguanLayer.removeFromParent(true);
    },

    //出牌的动作
    runChuActionOnline: function (tiles) {
        cc.log("runChuActionOnline");
        this.table.currentChuTile = tiles;

        var chuLayer = this.getChildByName("chuLayer");
        if (!chuLayer) {
            chuLayer = new cc.Layer();
            this.addChild(chuLayer, this.chuLineZOrder, "chuLayer");
        }

        chuLayer.removeAllChildren(true);

        this.setCurrentChuPosition(tiles.length);

        //var runningScene = cc.director.getRunningScene();
        //var gamePrepareLayer = runningScene.getChildByName(gpNamespace.GamePrepareLayerName);

        gpNamespace.SoundManager.playEffect(gpNamespace.soundRes.ChupaiEffect, false);

        for (var index = 0; index < tiles.length; index++) {
        	cc.log("tiles[index]" + tiles[index]);
            var tile = tiles[index];
            tile.setStatus(tyNamespace.TileStatus.Da);
            tile.setBelong(this.player);
//            this.player.tableTiles.push(tile);
            var line = parseInt(index / this.lineCount);
            var lineIndex = index % this.lineCount;
            this.setChuPosition(lineIndex, line);
            
            cc.log("tile.getImg():" + tile.getImg());
            var showTile = new cc.Sprite(tile.getImg());
            showTile.setPosition(this.chuPoint);
            chuLayer.addChild(showTile, index, this.showTileTag);
            showTile.setVisible(false);

            //var tileIndex = gpNamespace.CardActions.getTileIndex(tile, this.player.handTiles);

            var startPoint = this.fromPoint;
            var endPoint = this.chuPoint;
            
            cc.log("startPoint:" + startPoint);
            cc.log("endPoint:" + endPoint);
            
            var moveTile = new cc.Sprite(tile.getImg());
            
            moveTile.setPosition(startPoint);
            chuLayer.addChild(moveTile);
            moveTile.runAction(cc.sequence(cc.moveTo(0.2, endPoint), cc.delayTime(0.2), cc.callFunc(function () {
                showTile.setVisible(true);
            })));
        }

//        this.table.updateCardsRemain();//更新记牌器

        cc.log("runChuActionOnline end");
    },
    
    noChu: function() {
    	//不出标签
    	var nochuLabel = new cc.Sprite(gpNamespace.pdkgame.Game_nochu);
    	nochuLabel.setPosition(this.chuPointStart);
    	this.addChild(nochuLabel,111);
    	
    	nochuLabel.runAction(cc.sequence(cc.delayTime(2), cc.callFunc(function () {
    		nochuLabel.removeFromParent(true);
    	})));
    },

    setCurrentChuPosition: function (length) {
        this.currentChuPosition = cc.p(this.chuPointStart.x, this.chuPointStart.y);
        this.currentChuPosition.x = this.chuPointStart.x - 30 * (length - 1) / 2;
    },

    setChuPosition: function (i, line) {
        this.chuPoint.x = this.currentChuPosition.x + 30 * i;
        this.chuPoint.y = this.currentChuPosition.y - 60 * line;
    },

    //出牌后重置牌的位置
    resetTilesPositionNormal: function () {
        var myTiles = this.player.handTiles;

        for (var i = 0; i < myTiles.length; i++) {
            var tile = myTiles[i];

            tile.clickTime = 0;
            tile.sprite.setLocalZOrder(i);
            this.setTileSpritePosition(i, tile.sprite);
        }
    },

    //设置牌位置
    setTileSpritePosition: function (i, sprite) {
        sprite.setPosition(this.tileSpace * i + this.startPoint.x + this.tileSpace / 2, this.startPoint.y);
    },

    drawScoreLayer: function (score) {
//        var scoreLayer = new gpNamespace.ScoreLayer(score);
//        scoreLayer.setAnchorPoint(0, 0.5);
//
//        this.setScoreLayerPosition(scoreLayer);
//
//        this.table.addChild(scoreLayer, 1100, "scoreLayer");

        this.player.userItem.lScore += score;

        if (this.player.userItem.lScore < 0)
            this.player.userItem.lScore = 0;
        
        var runningScene = cc.director.getRunningScene();
        var prepareLayer = runningScene.getChildByName(gpNamespace.GamePrepareLayerName);
        cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkgamesenceRes.Pdkgamesence_plist);
        cc.spriteFrameCache.addSpriteFrames(gpNamespace.gameRes.Game_plist);
        if (prepareLayer) {
        	var myHeadLayer = prepareLayer.getChildByName("headLayer" + this.player.orderIndex);
        	
        	if (!myHeadLayer)
        		return;
        	
        	var myGoldLayer = myHeadLayer.getChildByName("goldLayer");
        	if (myGoldLayer) {
        		var countCell = myGoldLayer.getChildByName("gold");
        		
        		if (countCell)
        			countCell.reloadNum(this.player.userItem.lScore);
        	}
        }
    },

    setScoreLayerPosition: function (scoreLayer) {
        scoreLayer.setPosition(this.headPoint.x, this.headPoint.y + 50);
    },
    
    setScore: function (score) {
    	var runningScene = cc.director.getRunningScene();
    	var prepareLayer = runningScene.getChildByName(gpNamespace.GamePrepareLayerName);
    	cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkgamesenceRes.Pdkgamesence_plist);

    	if (prepareLayer) {
    		var myHeadLayer = prepareLayer.getChildByName("headLayer" + this.player.orderIndex);

    		if (!myHeadLayer)
    			return;

    		var myGoldLayer = myHeadLayer.getChildByName("goldLayer");
    		if (myGoldLayer) {
    			var countCell = myGoldLayer.getChildByName("gold");

    			if (countCell)
    				countCell.reloadNum(score);
    		}
    	}
    }
});